home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / nurb_polyg / GGVecLib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  3.3 KB  |  137 lines

  1. /*
  2. 2d and 3d Vector C Library
  3. by Andrew Glassner
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6. #include <stdlib.h>
  7.  
  8. #include <math.h>
  9. #include "GraphicsGems.h"
  10.  
  11. /******************/
  12. /*   3d Library      */
  13. /******************/
  14.  
  15. /* returns squared length of input vector */
  16. double V3SquaredLength(Vector3 *a)
  17. {
  18.     return((a->x * a->x)+(a->y * a->y)+(a->z * a->z));
  19. }
  20.  
  21. /* returns length of input vector */
  22. double V3Length(Vector3 *a)
  23. {
  24.     return(sqrt(V3SquaredLength(a)));
  25.     }
  26.  
  27. /* negates the input vector and returns it */
  28. Vector3 *V3Negate(Vector3 *v)
  29. {
  30.     v->x = -v->x;  v->y = -v->y;  v->z = -v->z;
  31.     return(v);
  32.     }
  33.  
  34. /* normalizes the input vector and returns it */
  35. Vector3 *V3Normalize(Vector3 *v)
  36. {
  37. double len = V3Length(v);
  38.     if (len != 0.0) { v->x /= len;  v->y /= len; v->z /= len; }
  39.     return(v);
  40.     }
  41.  
  42. /* scales the input vector to the new length and returns it */
  43. Vector3 *V3Scale(Vector3 *v, double newlen)
  44. {
  45. double len = V3Length(v);
  46.     if (len != 0.0) {
  47.     v->x *= newlen/len;      v->y *= newlen/len;  v->z *= newlen/len;
  48.     }
  49.     return(v);
  50.     }
  51.  
  52.  
  53. /* return vector sum c = a+b */
  54. Vector3 *V3Add(Vector3 *a, Vector3 *b, Vector3 *c)
  55. {
  56.     c->x = a->x+b->x;  c->y = a->y+b->y;  c->z = a->z+b->z;
  57.     return(c);
  58.     }
  59.  
  60. /* return vector difference c = a-b */
  61. Vector3 *V3Sub(Vector3 *a, Vector3 *b, Vector3 *c)
  62. {
  63.     c->x = a->x-b->x;  c->y = a->y-b->y;  c->z = a->z-b->z;
  64.     return(c);
  65.     }
  66.  
  67. /* return the dot product of vectors a and b */
  68. double V3Dot(Vector3 *a, Vector3 *b)
  69. {
  70.     return((a->x*b->x)+(a->y*b->y)+(a->z*b->z));
  71.     }
  72.  
  73. /* linearly interpolate between vectors by an amount alpha */
  74. /* and return the resulting vector. */
  75. /* When alpha=0, result=lo.  When alpha=1, result=hi. */
  76. Vector3 *V3Lerp(Vector3 *lo, Vector3 *hi, double alpha, Vector3 *result)
  77. {
  78.     result->x = LERP(alpha, lo->x, hi->x);
  79.     result->y = LERP(alpha, lo->y, hi->y);
  80.     result->z = LERP(alpha, lo->z, hi->z);
  81.     return(result);
  82.     }
  83.  
  84. /* make a linear combination of two vectors and return the result. */
  85. /* result = (a * ascl) + (b * bscl) */
  86. Vector3 *V3Combine (Vector3 *a, Vector3 *b, Vector3 *result, double ascl, double bscl)
  87. {
  88.     result->x = (ascl * a->x) + (bscl * b->x);
  89.     result->y = (ascl * a->y) + (bscl * b->y);
  90.     result->y = (ascl * a->z) + (bscl * b->z);
  91.     return(result);
  92.     }
  93.  
  94.  
  95. /* multiply two vectors together component-wise and return the result */
  96. Vector3 *V3Mul (Vector3 *a, Vector3 *b, Vector3 *result)
  97. {
  98.     result->x = a->x * b->x;
  99.     result->y = a->y * b->y;
  100.     result->z = a->z * b->z;
  101.     return(result);
  102.     }
  103.  
  104. /* return the distance between two points */
  105. double V3DistanceBetween2Points(Point3 * a, Point3 * b)
  106. {
  107. double dx = a->x - b->x;
  108. double dy = a->y - b->y;
  109. double dz = a->z - b->z;
  110.     return(sqrt((dx*dx)+(dy*dy)+(dz*dz)));
  111.     }
  112.  
  113. /* return the cross product c = a cross b */
  114. Vector3 *V3Cross(Vector3 *a, Vector3 *b, Vector3 *c)
  115. {
  116.     c->x = (a->y*b->z) - (a->z*b->y);
  117.     c->y = (a->z*b->x) - (a->x*b->z);
  118.     c->z = (a->x*b->y) - (a->y*b->x);
  119.     return(c);
  120.     }
  121.  
  122. /* create, initialize, and return a new vector */
  123. Vector3 *V3New(double x, double y, double z)
  124. {
  125. Vector3 *v = NEWTYPE(Vector3);
  126.     v->x = x;  v->y = y;  v->z = z;
  127.     return(v);
  128.     }
  129.  
  130. /* create, initialize, and return a duplicate vector */
  131. Vector3 *V3Duplicate(Vector3 *a)
  132. {
  133. Vector3 *v = NEWTYPE(Vector3);
  134.     v->x = a->x;  v->y = a->y;    v->z = a->z;
  135.     return(v);
  136.     }
  137.